<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<script
	src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<title>Welcome</title>
</head>
<body>
	<h2>
		<a href="<c:url value="out"/>">out</a>
	</h2>
	<h2>
		<a href="<c:url value="if"/>">if</a>
	</h2>
	<div id="weather-temp">default</div>

	<button id="boton">boton</button>

	<script type="text/javascript">
		$(document).ready(function() {
			// 			console.log("ready!");
			// 			console.log($.now());
		});

		// Shorthand for $( document ).ready()
		$(function() {
			// 			console.log("ready!");
		});

		/* 
		//Esta funcion es para hacer una peticion ajax en un click
		$("#boton").click(function() {
			console.log("click event");
			$.ajax({
				url : "/JSTL/ajax",
				data : {
					user : "jose"
				},
				success : function(data) {
					$("#weather-temp").html(data);
				}
			});
		});
		 */

		//esta funcion es como la anterior pero antes espera a que el dom este listo
		$(document).ready($("#boton").click(function() {
			// 			console.log($.now());
			// 			console.log("click event");
			$.ajax({
				url : "/JSTL/ajax",
				data : {
					user : "jose"
				},
				success : function(data) {
					$("#weather-temp").html(data);
				}
			});
		}));
	</script>

	<form action="/JSTL/ajax" id="searchForm">
		<input type="text" name="name" placeholder="nombre..."> 
		<input type="text" name="lastname" placeholder="apellido..."> 
		<input type="submit" value="Search">
	</form>

	<div id="result"></div>

	<script>
		// Attach a submit handler to the form
		$("#searchForm").submit(function(event) {

			// Stop form from submitting normally
			event.preventDefault();

			 $.post($('#searchForm').attr("action"), $('#searchForm').serialize(), function(data) {
				 $("#weather-temp").html(data);
		       }
		    );

			// Get some values from elements on the page:
			var $form = $(this);
			var term = $form.find("input[name='name']").val();
			console.log(term);
			var url = $form.attr("action");
			console.log(url);

			// Send the data using post
			var posting = $.post(url, {
				s : term
			});

			// Put the results in a div
			posting.done(function(data) {
				var content = $(data).find("#content");
				$("#result").empty().append(content);
			});
		});
	</script>

</body>
</html>


